#include "DragDetector.h"

#include "Misc/StdHead.h"

#include "Widgets/Mouse/MouseWidget.h"
#include "Widgets/Window.h"

void CDragDetector::StartDetect(TWeakObjectPtr<RMouseWidget> InDetectingTarget,EMouseButton InButton)
{
    //assert(!bIsDetecting && "there is only one drag detecting at same time");
    //bIsDetecting=true;

    assert(!DetectingDragging.IsValid() && "there is only one drag detecting at same time");

    DetectingDragging=InDetectingTarget;
    Button=InButton;
    
    //InitialMousePosition=SMouseButtonEvent::GetMosuePositionOnScreen();

    DetectingDragging->GetWindow()->StartCaptureMouse();

}

bool CDragDetector::Detect(const  SMouseButtonEvent& InMouseEvent)
{
    assert(DetectingDragging.IsValid() && "is not detecting any dragging widget");
  
    switch(InMouseEvent.GetType())
    {
        case EMouseEventType::Move:
        {
            //assert(SMouseButtonEvent::GetMosuePositionOnScreen()!=InitialMousePosition);

            StopDetect();
            return true;  //detected !
        }
        break;
        case EMouseEventType::ButtonUp:
            StopDetect();
            return false;
        break;

        default:
        break;
    }
    
    
    return false;
}

bool CDragDetector::IsDetecting() const 
{
    return DetectingDragging.IsValid();
}

TWeakObjectPtr<RMouseWidget> CDragDetector::GetTarget() const
{
    return DetectingDragging;
}

void CDragDetector::StopDetect()
{
    DetectingDragging->GetWindow()->StopCaptureMouse();
    DetectingDragging=nullptr;
}

// CDragDetector& CDragDetector::Get()
// {
//     CDragDetector Instance=CDragDetector();
//     return Instance;
// }
